package pro.softzhang.algo.lc300;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class LC380_InsertDeleteGetrandomO1 {
    public static void main(String[] args) {
         RandomizedSet obj = new RandomizedSet();
         boolean param_1 = obj.insert(1);
         boolean param_2 = obj.remove(1);
         boolean param_3 = obj.insert(2);
         boolean param_4 = obj.insert(3);
         boolean param_6 = obj.insert(4);
         int param_5 = obj.getRandom();
        System.out.println(param_5);
    }

    static class RandomizedSet {
        private Map<Integer, Integer> map;
        private Object[] array;

        public RandomizedSet() {
            map = new HashMap<>();
        }

        public boolean insert(int val) {
            if(map.containsKey(val)) {
                return false;
            }
            map.put(val, val);
            array = Arrays.stream(map.keySet().toArray()).toArray();
            return true;
        }

        public boolean remove(int val) {
            boolean f = map.remove(val) == null ? false : true;
            array = Arrays.stream(map.keySet().toArray()).toArray();
            return f;
        }

        public int getRandom() {
            int idx = new Random().nextInt(array.length);
            return (int)array[idx];
        }
    }
}
